package com.lh.common.utils;

import com.lh.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2019/11/13 16:33
 * @Author GaoZien
 * @Desc 工具类
 */
@Slf4j
public class CommUtils {

    /**
     * 分割list
     *
     * @param totallist
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> totallist) {

        //定义每次传输条数
        int count = 1000;
        int totalCount = totallist.size();
        //计算循环次数
        int num = totalCount % count == 0 ? totalCount / count : totalCount / count + 1;
        //while循环判断标志，每次循环后加1，不能大于num
        int flag = 0;
        //list使用subList()方法的起始参数
        int fromIndex = 0;
        //list使用subList()方法的结束参数
        int toIndex = 0;
        //定义返回对象
        List<List<T>> totalList = new ArrayList<>(num);
        while (flag < num) {
            fromIndex = toIndex;
            toIndex = fromIndex + count;
            //定义分割后的接受对象
            List<T> subList = new ArrayList<>();
            if (toIndex < totalCount) {
                subList = totallist.subList(fromIndex, toIndex);
            } else {
                subList = totallist.subList(fromIndex, totalCount);
            }
            totalList.add(subList);
            flag++;
        }
        return totalList;
    }

    public static Long parseLong(String idStr) {
        Long result = 0L;
        try {
            result = Long.parseLong(idStr);
        } catch (NumberFormatException e) {}
        return result;
    }

    /**
     * 校验手机号
     * @param phone
     */
    public static void checkPhone(String phone) {
        //检查手机号是否正确
        phone = phone.trim();
        boolean isBool = RegexUtils.checkMobile(phone);;
        if (!isBool) {
            throw new RRException("手机号码有误!");
        }

    }
}
